import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class ExportService extends BaseService<any> {
  private readonly logger = new Logger(ExportService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: ExportService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'id, export_key, export_num, file_path, file_size, export_status, create_time';         $search_model = $this->model->where([['site_id', '=',...
const field = 'id, export_key, export_num, file_path, file_size, export_status, create_time';
        const search_model = this.model.where([['site_id', '=', this.site_id))).withSearch(['export_key', 'export_status', 'create_time'),where).append(['export_key_name', 'export_status_name')).field(field).order('id desc'];
        return this.pageQuery(search_model);
    }

    /**
     * 获取导出数据类型列表
     * @return */
    async getExportDataType()
    {
        return this.coreExportService.getExportDataType(];
    }

    /**
     * 检查导出数据源是否为空
     * @param string type
     * @param where
     * @return bool
     */
    async checkExportData(string type, where)
    {
        const page = this.request.params([
            ['page', 0],
            ['limit', 0)
        )];
        const data = this.coreExportService.getExportData(this.site_id, type, where, page);
        return count(data) > 0;
    }

    /**
     * 报表导出
     * @param string type
     * @param where
     * @return bool
     */
    async exportData(string type, where){
        const page = this.request.params([
            ['page', 0],
            ['limit', 0)
        )];
        ExportJob.dispatch([site_id: this.site_id, type: type, where: where, page: page)];
        return true;
    }

    /**
     * 报表删除
     * @param int id
     * @return bool
     */
    async deleteExport(int id)
    {
        const export = this.model.where([['id', '=', id], ['site_id', '=', this.site_id))).find(];
        if (!!export.file_path)) this.coreExportService.deleteExportFile(export.file_path];
        const res = export.delete();
        return res;
    }
}
  }

  /**
   * getExportDataType
   * 对应 PHP: ExportService_admin::getExportDataType()
   * 逻辑类型: undefined - undefined
   */
  async getExportDataType() {
    // 基于PHP真实逻辑: getExportDataType
    // PHP原文: return (new CoreExportService())->getExportDataType();     }      /**      * 检查导出数据源是否为空      * @param string $type      * @param array $where      * ...
return this.coreExportService.getExportDataType(];
    }

    /**
     * 检查导出数据源是否为空
     * @param string type
     * @param where
     * @return bool
     */
    async checkExportData(string type, where)
    {
        const page = this.request.params([
            ['page', 0],
            ['limit', 0)
        )];
        const data = this.coreExportService.getExportData(this.site_id, type, where, page);
        return count(data) > 0;
    }

    /**
     * 报表导出
     * @param string type
     * @param where
     * @return bool
     */
    async exportData(string type, where){
        const page = this.request.params([
            ['page', 0],
            ['limit', 0)
        )];
        ExportJob.dispatch([site_id: this.site_id, type: type, where: where, page: page)];
        return true;
    }

    /**
     * 报表删除
     * @param int id
     * @return bool
     */
    async deleteExport(int id)
    {
        const export = this.model.where([['id', '=', id], ['site_id', '=', this.site_id))).find(];
        if (!!export.file_path)) this.coreExportService.deleteExportFile(export.file_path];
        const res = export.delete();
        return res;
    }
}
  }

  /**
   * checkExportData
   * 对应 PHP: ExportService_admin::checkExportData()
   * 逻辑类型: undefined - undefined
   */
  async checkExportData(type: any[], where: any[]) {
    // 基于PHP真实逻辑: checkExportData
    // PHP原文: $page = $this->request->params([             ['page', 0],             ['limit', 0]         ]);         $data = (new CoreExportService())->getExportDat...
const page = this.request.params([
            ['page', 0],
            ['limit', 0)
        )];
        const data = this.coreExportService.getExportData(this.site_id, type, where, page);
        return count(data) > 0;
    }

    /**
     * 报表导出
     * @param string type
     * @param where
     * @return bool
     */
    async exportData(string type, where){
        const page = this.request.params([
            ['page', 0],
            ['limit', 0)
        )];
        ExportJob.dispatch([site_id: this.site_id, type: type, where: where, page: page)];
        return true;
    }

    /**
     * 报表删除
     * @param int id
     * @return bool
     */
    async deleteExport(int id)
    {
        const export = this.model.where([['id', '=', id], ['site_id', '=', this.site_id))).find(];
        if (!!export.file_path)) this.coreExportService.deleteExportFile(export.file_path];
        const res = export.delete();
        return res;
    }
}
  }

  /**
   * exportData
   * 对应 PHP: ExportService_admin::exportData()
   * 逻辑类型: undefined - undefined
   */
  async exportData(type: any[], where: any[]) {
    // 基于PHP真实逻辑: exportData
    // PHP原文: $page = $this->request->params([             ['page', 0],             ['limit', 0]         ]);         ExportJob::dispatch(['site_id' => $this->site_i...
const page = this.request.params([
            ['page', 0],
            ['limit', 0)
        )];
        ExportJob.dispatch([site_id: this.site_id, type: type, where: where, page: page)];
        return true;
    }

    /**
     * 报表删除
     * @param int id
     * @return bool
     */
    async deleteExport(int id)
    {
        const export = this.model.where([['id', '=', id], ['site_id', '=', this.site_id))).find(];
        if (!!export.file_path)) this.coreExportService.deleteExportFile(export.file_path];
        const res = export.delete();
        return res;
    }
}
  }

  /**
   * deleteExport
   * 对应 PHP: ExportService_admin::deleteExport()
   * 逻辑类型: undefined - undefined
   */
  async deleteExport(id: number) {
    // 基于PHP真实逻辑: deleteExport
    // PHP原文: $export = $this->model->where([['id', '=', $id], ['site_id', '=', $this->site_id]])->find();         if (!empty($export->file_path)) (new CoreExportSe...
const export = this.model.where([['id', '=', id], ['site_id', '=', this.site_id))).find(];
        if (!!export.file_path)) this.coreExportService.deleteExportFile(export.file_path];
        const res = export.delete();
        return res;
    }
}
  }
}
